using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Runtime.InteropServices;
using System.IO;

namespace SharpCast
{
    [Serializable(), ClassInterface(ClassInterfaceType.AutoDual)]
    public class Episode : Common 
    {
        private Channel _parent;
        
        private string _title;
        private TimeSpan _duration;
        private Uri _link;
        private DateTime _published;
        private string _guid;
        private string[] _categories;
        private string[] _keywords;
        private string _subtitle;
        private string _author;
        private string _summary;

        // media:content
        private Uri _url;
        private long _fileSize;
        private string _contentType;
        
        public Episode(XmlElement item, Channel parent)
        {
            element = item;

            // Update fields
            _parent = parent;

            _title = getValue("title");
            _subtitle = getValue("itunes:subtitle");
            _summary = getValue("itunes:summary");
            try
            {
                _link = new Uri(getChild("link").InnerText);
            }
            catch
            {
                _link = null;
            }
            try
            {
                _duration = TimeSpan.Parse(getChild("itunes:duration").InnerText);
            }
            catch
            {
                _duration = new TimeSpan(0);
            }
            try
            {
                _published = DateTime.Parse(getChild("pubDate").InnerText);
            }
            catch
            {
                _published = new DateTime(0, DateTimeKind.Unspecified);
            }
            _guid = getValue("guid");

            XmlNodeList tags = element.GetElementsByTagName("category");
            _categories = new string[tags.Count];
            for (int i = 0; i < tags.Count; i++)
            {
                _categories[i] = tags[i].InnerText;
            }

            _keywords = getValue("itunes:keywords").Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
            _author = getValue("itunes:author");

            //XmlElement media = getChild("media:content");
            //_url = new Uri(media.Attributes["url"].Value);
            //_fileSize = long.Parse(media.Attributes["fileSize"].Value);
            //_contentType = media.Attributes["type"].Value;
            XmlElement enclosure = getChild("enclosure");
            _url = new Uri(enclosure.Attributes["url"].Value);
            try
            {
                _fileSize = long.Parse(enclosure.Attributes["length"].Value);
            }
            catch { }

            _contentType = enclosure.Attributes["type"].Value;
        }

        public string title
        {
            get { return _title; }
        }

        public TimeSpan duration
        {
            get { return _duration; }
        }

        public Uri link
        {
            get { return _link; }
        }

        public Uri url
        {
            get { return _url; }
        }

        public long fileSize
        {
            get { return _fileSize; }
        }

        public string summary
        {
            get { return _summary; }
        }

        public string guid
        {
            get
            {
                if (_guid != null)
                    return _guid;
                else
                    return _url.ToString();
            }
        }

        public Channel channel
        {
            get { return _parent; }
        }

        /// <summary>
        /// Start an asynchrnonous download of this episode to the specified location
        /// </summary>
        /// <param name="location"></param>
        public DownloadHandle download(string location)
        {
            return new DownloadHandle(this, new FileInfo(location));
        }
    }
}
